package com.qiniu.pili.droid.rtcstreaming.demo.main;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.qiniu.pili.droid.rtcstreaming.RTCStartConferenceCallback;
import com.qiniu.pili.droid.rtcstreaming.RTCStreamingManager;
import com.qiniu.pili.droid.rtcstreaming.demo.R;
import com.qiniu.pili.droid.rtcstreaming.demo.test.AudioCapturer;
import com.qiniu.pili.droid.streaming.AVCodecType;
import com.qiniu.pili.droid.streaming.MicrophoneStreamingSetting;

/**
 * 观众activity，只有连麦功能
 *
 * @author Leo
 *         created at 2018/1/23 下午4:57
 */
public class AudienceActivity extends AppCompatActivity implements View.OnClickListener {

    private Button mAddConference;
    private Button mQuitConference;
    private AudioCapturer mAudioCapturer;
    private ProgressDialog mProgressDialog;
    private RTCStreamingManager mStreamingManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audience);
        initView();
        initStream();
    }

    private void initStream() {
        RTCStreamingManager.init(getApplicationContext());
        mStreamingManager = new RTCStreamingManager(this, AVCodecType.HW_VIDEO_YUV_AS_INPUT_WITH_HW_AUDIO_CODEC);
        MicrophoneStreamingSetting setting = new MicrophoneStreamingSetting();

        mAudioCapturer = new AudioCapturer();
    }

    private void initView() {
        mAddConference = (Button) findViewById(R.id.add_conference);
        mQuitConference = (Button) findViewById(R.id.quit_conference);

        mAddConference.setOnClickListener(this);
        mQuitConference.setOnClickListener(this);
        mProgressDialog = new ProgressDialog(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
//        mConferenceManager.startAudioCapture();
    }

    @Override
    protected void onPause() {
        super.onPause();
//        mConferenceManager.stopAudioCapture();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mStreamingManager.destroy();
        RTCStreamingManager.deinit();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_conference:
                mProgressDialog.setMessage("正在加入连麦。。。");
                mProgressDialog.show();
                mAudioCapturer.startCapture();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        addConference();
                    }
                }).start();
                break;
            case R.id.quit_conference:
                quitConference();
                break;
        }
    }

    private void quitConference() {
        mStreamingManager.stopConference();
        mAudioCapturer.stopCapture();
        mAudioCapturer.setOnAudioFrameCapturedListener(null);
    }

    private void addConference() {

        mStreamingManager.startConference("leo", Constant.roomId, Constant.roomToken, new RTCStartConferenceCallback() {
            @Override
            public void onStartConferenceSuccess() {
                try {
                    Thread.sleep(1000);
                    mAudioCapturer.setOnAudioFrameCapturedListener(new AudioCapturer.OnAudioFrameCapturedListener() {
                        @Override
                        public void onAudioFrameCaptured(byte[] audioData, long ts) {
                            if (audioData!=null){
                                mStreamingManager.inputAudioFrame(audioData,ts);
                            }
                        }
                    });

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            showToast("加入连麦成功！");
                            mProgressDialog.dismiss();
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onStartConferenceFailed(int i) {
                showToast("加入连麦失败！ 错误码为：  " + i);
            }
        });

    }

    private void showToast(String s) {
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
    }
}
